--------------------------------------------
-- 充值-特权商店
-- 购买后开始计时 x天后刷新

-- db.tequanShop[id] = {cnt, time}
--------------------------------------------

local TequanShopExcel = require("excel.present").tequanShop
local Util = require("common.Util")
local Lang = require("common.Lang")
local Msg = require("core.Msg")
local ObjHuman = require("core.ObjHuman")
local Broadcast = require("broadcast.Broadcast")
local Grid = require("bag.Grid")
local BagLogic = require("bag.BagLogic")
local BuyLogic = require("topup.BuyLogic")


-- 花费类型
COST_RMB = 1
COST_ZUANSHI = 2

-- 剩余永久
local DEF_FOREVER_TIME = -1

-- 是否激活征战扫荡特权
function isActive(human, YYInfo, funcConfig)
   return isActiveMopup(human)
end

-- 是否激活征战扫荡特权
function isActiveMopup(human)
	if not human.db.tequanMopup then
		return
	end
    if human.db.tequanMopup < os.time() then
		human.db.tequanMopup = nil
		return 
	end
	return true
end

-- 已购买次数
function getCnt(human, id)
	if not human.db.tequanShop then
		return 0
	end
	local data = human.db.tequanShop[id]
	if not data then return 0 end
	return data.cnt or 0
end

-- 记录购买次数
function addCnt(human, id)
	if not human.db.tequanShop then
		human.db.tequanShop = {}
	end
	if not human.db.tequanShop[id] then
		human.db.tequanShop[id] = {}
	end
	local data = human.db.tequanShop[id]
	data.cnt = (data.cnt or 0) + 1
	data.time = data.time or os.time()
end

-- 剩余购买次数
function getLeftCnt(human, id)
	local config = TequanShopExcel[id]
	if not config then
		return 0
	end	
	return math.max(config.limitCnt - getCnt(human, id), 0)
end

-- 刷新剩余时间
function getLeftTime(human, id)	
	local config = TequanShopExcel[id]
	if not config then
		return DEF_FOREVER_TIME
	end
	if not human.db.tequanShop then
		return 0
	end
	local data = human.db.tequanShop[id]
	if not data then return 0 end

	local leftTime = DEF_FOREVER_TIME
	if config.refreshTime > 0 then
		local refreshTimeTs = config.refreshTime * 86400
		leftTime = math.max(refreshTimeTs - (os.time() - data.time), 0)
	end
	return leftTime
end

-- 检查是否需要刷新
function checkData(human)
	if not human.db.tequanShop then
		return
	end

	for id in pairs(human.db.tequanShop) do
		if getLeftTime(human, id) == 0 then
			human.db.tequanShop[id] = nil
		end		
	end
end

-- 封装礼包结构体
function fontShopNet(net, id, config, human)
	local leftTime = getLeftTime(human, id)	
	local leftCnt = getLeftCnt(human, id)
	net.id = id 
	net.buyItem[0] = 0
	net.needZuanshi = 0
	local costType = config.cost[1]
	if costType == COST_RMB then
		net.buyItem[0] = 1
		BuyLogic.fontBuyItem(human, net.buyItem[1], config.cost[2])
	else
		net.needZuanshi = config.cost[2]
	end
	net.cnt = getCnt(human, id)
	net.maxCnt = config.limitCnt
	net.name = config.name
    local len = #config.items
    for i = 1,len do
        Grid.makeItem(net.item[i],config.items[i][1],config.items[i][2])
    end
    net.item[0] = len
	net.desc = config.desc
	net.leftTime = math.max(leftTime, 0)
	net.limitDesc = config.limitDesc
	return true
end

-- 发送
function sendQuery(human)
	checkData(human)

	local msgRet = Msg.gc.GC_TEQUANSHOP_QUERY
	msgRet.list[0] = 0
	for id, config in ipairs(TequanShopExcel) do
		local net = msgRet.list[msgRet.list[0] + 1]
		if fontShopNet(net, id, config, human) then
			msgRet.list[0] = msgRet.list[0] + 1
		end
	end
	--Msg.trace(msgRet)
	Msg.send(msgRet, human.fd)
end

-- 购买
function buyLibao(human, id, costType)
	local config = TequanShopExcel[id]
	if not config then return end

	if config.cost[1] ~= costType then
		return
	end

	checkData(human)
	if getLeftCnt(human, id) < 1 then
		return Broadcast.sendErr(human, Lang.YUNYING_BUY_ERR_CNT)
	end

	if costType == COST_ZUANSHI then
		local needZuanshi = config.cost[2]
		if not ObjHuman.checkRMB(human, needZuanshi) then
			return
		end
		ObjHuman.decZuanshi(human, -needZuanshi, "tequan_shop")
	end

	addCnt(human, id)
	BagLogic.addItemList(human, config.items, "tequan_shop")
	sendQuery(human)	
end
 